package com.pool.socket;

import java.io.*;
import java.net.*;
import java.util.*;
import com.pool.socket.DataType.*;

public class SocketServer {

	protected static HashMap<Socket, SocketDepict> socketPool = new HashMap<Socket, SocketDepict>();
	protected static Queue<DataHandler> handlers = new LinkedList<DataHandler>();
	public static HashMap<UUID, String> GlobalToLocalId = new HashMap<UUID, String>();
	public static HashMap<String, UUID> LocalIdToGlobal = new HashMap<String, UUID>();
	protected static HashMap<UUID, ArrayList<Socket>> GlobalIdToSocket = new HashMap<UUID, ArrayList<Socket>>();
	protected static LinkedList<VirtualGateway> gateways = new LinkedList<VirtualGateway>();
	private static SocketServer singleton = null;

	protected static DataSender sender = new DataSender();

	public static SocketServer getInstance() {
		if (singleton == null)
			singleton = new SocketServer();
		sender.start();
		return singleton;
	}

	public SocketServer() {
	}

	static public Queue<DataHandler> getHandlers() {
		return handlers;
	}

	static public LinkedList<VirtualGateway> getGateways() {
		return gateways;
	}

	static public VirtualGateway getGatewayById(int gatewayId) {
		for (int i = 0; i < gateways.size(); i++) {
			VirtualGateway gateway = gateways.get(i);
			if (gateway.getGatewayID() == gatewayId)
				return gateway;
		}
		return null;
	}

	static public HashMap<Socket, SocketDepict> getSocketPool() {
		return socketPool;
	}

	public static void main(String[] args) {
		SocketServer server = SocketServer.getInstance();
		MessageFormat.initialize();
		SocketListener listener = new SocketListener(server, 30003);
		listener.start();
		//SocketObserver observer = new SocketObserver(server, 5000);
		//observer.start();

	}
}
